// ****220v并网代码***** //

// 串口0 RX PB9 TX PB8
// SPWM输出 PA0 PA1
// tim1 PA2 频率捕获
// ADC采样 PA04

#include "ddl.h"
#include "bt.h"
#include "gpio.h"
#include "flash.h"
#include "adc.h"

#include "spwm_timer0.h"
#include "spwm_int.h"
#include "capture_timer1.h"
#include "sample_adc.h"
#include "uart_conf.h"
#include "uart.h"

#include "hdiv.h"

//时钟配置初始化
void App_ClockCfg(void)
{
    en_flash_waitcycle_t enWaitCycle;
    stc_sysctrl_pll_cfg_t stcPLLCfg;

    //结构体初始化清零
    DDL_ZERO_STRUCT(stcPLLCfg);

    enWaitCycle = FlashWaitCycle1;
    Flash_WaitCycle(enWaitCycle);

    stcPLLCfg.enInFreq = SysctrlPllInFreq4_6MHz;     //RCH 4MHz
    stcPLLCfg.enOutFreq = SysctrlPllOutFreq36_48MHz; //PLL 输出48MHz
    stcPLLCfg.enPllClkSrc = SysctrlPllRch;           //输入时钟源选择RCH
    stcPLLCfg.enPllMul = SysctrlPllMul12;            //4MHz x 12 = 48MHz
    Sysctrl_SetPLLFreq(&stcPLLCfg);
    Sysctrl_SetPLLStableTime(SysctrlPllStableCycle16384);
    Sysctrl_ClkSourceEnable(SysctrlClkPLL, TRUE);

    Sysctrl_SysClkSwitch(SysctrlClkPLL); ///< 时钟切换
}

void delay_ms(uint16_t ms)
{
    int i,j;
    for(i=0;i<ms;i++)
      for(j=0;j<1000;j++);
}

volatile uint32_t u32AdcRestult;
unsigned char msg[15]={0};
unsigned char msgLen = 0;
unsigned short freq= 50;
unsigned short freq_1=0;

stc_div_unsigned_result_t result_div_1 = {0, 0};

int32_t main(void)
{
	  freq=50;
	  int a =0;
	  int b =0;
	  int c =0;

    App_ClockCfg(); //时钟初始化
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralHdiv, TRUE);
	
    sineValue_update();
    
    App_Timer0Cfg(TimerArr_50Hz, 2400, 2400); //Timer0 配置:周期 4800(5K); 通道A比较值2400; 通道B比较值2400

    App_Timer0PortCfg(); //Timer3 Port端口配置

    Bt_M23_EnPWM_Output(TIM0, TRUE, FALSE); //TIM0 端口输出使能

    Bt_M23_Run(TIM0); //TIM0 运行
    
    App_Timer1PortCfg();    //端口配置初始化
    
    App_Timer1Cfg();        //Timer0配置初始化

    Bt_M1_Run(TIM1);        //TIM1 运行
    
    ///< ADC采样端口初始化
    App_AdcPortInit();
    
    ///< ADC模块 初始化
    App_AdcInit();
    
    ///< ADC 单次采样模式 配置
    App_AdcSglCfg();
    
    //串口引脚配置
    uart_gpioconf();
    
    //串口配置
    uart_conf();
    
    while(1)
    {    
        //采样电流
		if(TRUE == Adc_GetIrqStatus(AdcMskIrqSgl)) // ADC单次转换完成 
        {
            u32AdcRestult = Adc_GetSglResult();
					
            Adc_ClrIrqStatus(AdcMskIrqSgl); // 清空寄存器的中断状态标志位
        }
        
        if(captureFlag)  // 判断是否发生,TIM1
        {
           captureFlag = 0;  
					
					 Hdiv_Unsigned(300000000, captureValue, &result_div_1);  // 执行有符号除法  150000000
					
           freq_1 = result_div_1.Quotient;	// 频率为上面除法后得到的商。result_div_1.Quotient是32位//freq_1=300000000/captureValue
				
				 	 a=(freq_1/100000)%10;//freq_1第6位
					
					 b=(freq_1/10000)%10;//freq_1第5位
					
					 c= freq_1/100;
				}
				
				if(a==0&&b==0&&c<freq+0.5&&c>freq-0.5)//去除freq_1的超频无效信号
				{
					 freq=freq_1/100;
						
					 AC_FREQ = freq;

				   SPWM_Freq = freq;
						
					 sineValue_update();
				}
			
				msg[0] = 'F';
        msg[1] = 'R';
        msg[2] = 'E';
        msg[3] = 'Q';
        msg[4] = ':';
        msg[5] = '0'+(freq_1/1000)%10;
        msg[6] = '0'+(freq_1/100)%10; 
        msg[7] = '0'+(freq_1/10)%10;
        msg[8] = '0'+freq_1%10;
        msg[9] = '\n';
        msgLen=10;
        UART_send_bytes(msg, msgLen);
					
			  sineValue_update(); 
					
				delay_ms(100);
				
        }	 
		
}
